﻿using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;

namespace QFramework
{
    public class DefaultItemKitSaveHandler : IItemKitSaveHandler
    {
        #region subclasses
        [Serializable]
        public class SlotGroupSaveData
        {
            public string Key;
            public List<SlotSaveData> SlotSaveDatas;
        }
        [Serializable]
        public class SlotSaveData
        {
            public string ItemKey;
            public int Count;
        }
        #endregion
        public void Save(Dictionary<string, SlotGroup> dictSlotGroup)
        {
            var keyString = string.Join("|", dictSlotGroup.Keys);
            PlayerPrefs.SetString("slot_group_keys", keyString);

            foreach (var slotGroup in dictSlotGroup.Values)
            {
                var slotGroupSaveData = new SlotGroupSaveData()
                {
                    Key = slotGroup.Key,
                    SlotSaveDatas = slotGroup.Slots.Select(slot => new SlotSaveData()
                    {
                        ItemKey = slot.Item?.GetKey,
                        Count = slot.Count,
                    }).ToList(),
                };
                var json = JsonUtility.ToJson(slotGroupSaveData);
                PlayerPrefs.SetString($"slot_group_{slotGroup.Key}", json);
            }
        }

        public void Load(Dictionary<string, SlotGroup> dictSlotGroup)
        {
            var keyString = PlayerPrefs.GetString("slot_group_keys", string.Empty);
            var keys = keyString.Split("|");
            foreach (var key in keys)
            {
                dictSlotGroup.TryGetValue(key, out SlotGroup slotGroup);
                slotGroup ??= ItemKit.CreateSlotGroup(key);

                var json = PlayerPrefs.GetString($"slot_group_{slotGroup.Key}", string.Empty);
                if (json.IsNullOrEmpty())
                {

                }
                else
                {
                    var slotGroupSaveData = JsonUtility.FromJson<SlotGroupSaveData>(json);
                    for (int i = 0; i < slotGroupSaveData.SlotSaveDatas.Count; i++)
                    {
                        var slotSaveData = slotGroupSaveData.SlotSaveDatas[i];
                        var item = slotSaveData.ItemKey.IsNullOrEmpty() ? null : ItemKit.DictItem[slotSaveData.ItemKey];
                        if (i < slotGroup.Slots.Count)
                        {
                            slotGroup.Slots[i].Item = item;
                            slotGroup.Slots[i].Count = slotSaveData.Count;
                            slotGroup.Slots[i].OnChanged.Trigger();
                        }
                        else
                        {
                            slotGroup.CreateSlot(item, slotSaveData.Count);
                        }
                    }
                }
            }
        }

        public void Clear()
        {
            var keyString = PlayerPrefs.GetString("slot_group_keys", string.Empty);
            var keys = keyString.Split("|");
            foreach (var key in keys)
            {
                PlayerPrefs.DeleteKey($"slot_group_{key}");
            }
            PlayerPrefs.DeleteKey($"slot_group_keys");
        }
    }
}
